import pymysql

from ..config_util import mysql_config


class DetailShopInfoMysqlConnectUtil(object):
    def __enter__(self):
        # 部分更新
        select_sql = "SELECT shop_id, region FROM shop_info WHERE lat IS NULL;"
        # 全量更新
        # select_sql = "SELECT shop_id, region FROM shop_info;"
        update_sql = ("UPDATE shop_info SET star_level={star_level!r}, comment_number={comment_number!r}, "
                      "detailed_address={detailed_address!r}, contact_way={contact_way!r}, "
                      "business_hours={business_hours!r}, another_name={another_name!r}, "
                      "lng={lng!r}, lat={lat!r} WHERE shop_id={shop_id!r}")
        self.connection = pymysql.connect(**mysql_config)
        self.cursor = self.connection.cursor()
        return self.connection, self.cursor, select_sql, update_sql

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.connection.close()
        self.cursor.close()
